package com.kool.kadmin.system.service;

import com.kool.kadmin.common.bean.ListResult;
import com.kool.kadmin.common.bean.PageListResult;
import com.kool.kadmin.common.bean.Result;
import com.kool.kadmin.common.util.PaginatorUtil;
import com.kool.kadmin.common.util.ResultUtil;
import com.kool.kadmin.system.bean.SysAutDo;
import com.kool.kadmin.system.bean.SysMenuAutDo;
import com.kool.kadmin.system.bean.SysRoleAutDo;
import com.kool.kadmin.system.dao.SysAutDao;
import com.kool.kadmin.system.dao.SysMenuAutDao;
import com.kool.kadmin.system.dao.SysRoleAutDao;
import com.kool.kadmin.system.request.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 权限管理
 *
 * @author luyu
 */
@Service
@Slf4j
public class AutService {
    @Resource
    private SysAutDao autDao;
    @Resource
    private SysRoleAutDao roleAutDao;
    @Resource
    private SysMenuAutDao menuAutDao;

    /**
     * 新增权限
     *
     * @param request
     * @return
     */
    public Result add(AddAutReq request) {
        SysAutDo autDo = new SysAutDo();
        autDo.setAutCode(request.getAutCode()).setAutName(request.getAutName());
        autDao.insert(autDo);
        return ResultUtil.success();
    }

    /**
     * 修改权限
     *
     * @param request
     * @return
     */
    public Result modify(ModifyAutReq request) {
        SysAutDo autDo = new SysAutDo();
        autDo.setId(request.getId()).setAutCode(request.getAutCode()).setAutName(request.getAutName());
        autDao.updateById(autDo);
        return ResultUtil.success();
    }

    /**
     * 删除权限
     *
     * @param request
     * @return
     */
    public Result delete(DeleteAutReq request) {
        autDao.deleteById(request.getId());
        return ResultUtil.success();
    }

    /**
     * 分页查询权限列表
     *
     * @param request
     * @return
     */
    public PageListResult<SysAutDo> getPage(GetAutPageReq request) {
        int currentPage = request.getCurrentPage();
        int pageSize = request.getPageSize();

        SysAutDo autDo = new SysAutDo();
        autDo.setAutCode(request.getAutCode()).setAutName(request.getAutName());
        int offset = PaginatorUtil.calculateOffset(currentPage, pageSize);
        int total = autDao.countTotal(autDo);

        List<SysAutDo> auts = autDao.getPage(request.getAutCode(), request.getAutName(), offset, pageSize);
        return ResultUtil.success(auts, currentPage, pageSize, total);
    }

    /**
     * 绑定角色权限
     *
     * @param request
     * @return
     */
    public Result autRole(AutRoleReq request) {
        String roleCode = request.getRoleCode();
        List<String> autCodes = request.getAutCodes();
        //删除角色已绑定权限
        roleAutDao.deleteByRoleCode(roleCode);

        if (autCodes == null || autCodes.size() == 0) {
            return ResultUtil.success();
        }
        List<SysRoleAutDo> roleAutDos = autCodes.stream().map(item -> {
            SysRoleAutDo roleAutDo = new SysRoleAutDo();
            roleAutDo.setRoleCode(roleCode).setAutCode(item);
            return roleAutDo;
        }).collect(Collectors.toList());
        //批量插入角色权限
        roleAutDao.batchInsert(roleAutDos);
        return ResultUtil.success();
    }

    /**
     * 绑定菜单权限
     *
     * @param request
     * @return
     */
    public Result bindMenuAut(BindMenuAutReq request) {
        String autCode = request.getAutCode();
        List<String> menuCodes = request.getMenuCodes();
        //删除权限绑定的菜单
        menuAutDao.deleteByAutCode(autCode);

        if (menuCodes == null || menuCodes.size() == 0) {
            return ResultUtil.success();
        }
        List<SysMenuAutDo> menuAutDos = menuCodes.stream().map(item -> {
            return new SysMenuAutDo().setAutCode(autCode).setMenuCode(item);
        }).collect(Collectors.toList());
        //批量插入菜单权限
        menuAutDao.batchInsert(menuAutDos);
        return ResultUtil.success();
    }

    /**
     * 查询角色已授权权限
     *
     * @param request
     * @return
     */
    public ListResult<SysAutDo> getRoleAut(GetRoleAutsReq request) {
        String roleCode = request.getRoleCode();
        List<SysAutDo> autDos = roleAutDao.getAutByRoleCode(roleCode);
        return ResultUtil.success(autDos);
    }

    /**
     * 查询所有权限
     *
     * @return
     */
    public ListResult<SysAutDo> getAllAuth() {
        List<SysAutDo> autDos = autDao.getAllAuth();
        return ResultUtil.success(autDos);
    }
}
